package com.novunda.platform.web;

import com.novunda.platform.common.utils.UserUtils;
import com.novunda.platform.common.web.controller.BaseController;
import com.novunda.platform.entities.Menu;
import com.novunda.platform.entities.User;
import com.novunda.platform.services.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author linfeng
 * @since 2016/8/31
 */
@Controller
public class PageController extends BaseController {
    /**
     * 登陆service
     */
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/to/{id}", method = RequestMethod.GET)
    public String toPage(@PathVariable("id") Long id, HttpSession session) {
        User user = UserUtils.getUser();
        // 如果已经登录，则跳转到管理首页
        if (user.getId() == null) {
            return "redirect:/";
        }
        Menu menu = UserUtils.getMenuById(id);
        if (menu != null) {
            session.setAttribute(CURRENT_PAGE, menu.getParentIds() + menu.getId());
            return "forward:" + menu.getHref();
        } else {
            return "redirect:/";
        }
    }

    @RequestMapping(value = "/errors/500")
    public String error500(HttpServletRequest request, HttpServletResponse response,
                           Model model, RedirectAttributes redirectAttributes) {

        model.addAttribute("status", request.getAttribute("javax.servlet.error.status_code"));
        model.addAttribute("reason", request.getAttribute("javax.servlet.error.message"));
        return "errors/500";
    }

    @RequestMapping(value = "/errors/400")
    public String error400(HttpServletRequest request, HttpServletResponse response,
                           Model model, RedirectAttributes redirectAttributes) {

        model.addAttribute("status", request.getAttribute("javax.servlet.error.status_code"));
        model.addAttribute("reason", request.getAttribute("javax.servlet.error.message"));
        return "errors/400";
    }

    @RequestMapping(value = "/errors/404")
    public String error404(HttpServletRequest request, HttpServletResponse response,
                           Model model, RedirectAttributes redirectAttributes) {

        model.addAttribute("status", request.getAttribute("javax.servlet.error.status_code"));
        model.addAttribute("reason", request.getAttribute("javax.servlet.error.message"));
        return "errors/404";
    }

    @RequestMapping(value = "/errors/403")
    public String error403(HttpServletRequest request, HttpServletResponse response,
                           Model model, RedirectAttributes redirectAttributes) {

        model.addAttribute("status", request.getAttribute("javax.servlet.error.status_code"));
        model.addAttribute("reason", request.getAttribute("javax.servlet.error.message"));
        return "errors/403";
    }

    @RequestMapping(value = "/errors/loginError")
    public String loginError(HttpServletRequest request, HttpServletResponse response,
                             Model model, RedirectAttributes redirectAttributes) {
        String message = (String) request.getAttribute("javax.servlet.error.message");
        model.addAttribute("message", message.split(":")[1]);
        model.addAttribute("qrcode", loginService.getQRCode());
        return "tcalogin";
    }

}
